from flask import Flask,request,jsonify
app = Flask(__name__)
import pandas as pd



frequest_itensets = pd.read_pickle('frequent_itemsets.pkl')
rules = pd.read_pickle('rule.pkl')

@app.route("/recommend", methods=['POST'])
def recommend():
    data = request.json.get('items',[])


    recommendDations = []
    for idx,rule in rules.iterrows():
        antecedents = list(rule['antecedents'])
        consequents = list(rule['consequents'])


        if set(antecedents).issubset(set(data)):
            recommendDations.extend(consequents)


    recommendDations = list(set(recommendDations) - set(data))

    return jsonify({'recommendDations': recommendDations})

if __name__ == "__main__":
    app.run()